package com.freeter.modules.gjs.util;

import java.io.IOException;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.Resource;

@Configuration
public class RedissonSpringDataConfig {

   @Bean
   public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
       return new RedissonConnectionFactory(redisson);
   }

   @Bean(destroyMethod = "shutdown")
   @Profile("dev")
   public RedissonClient redissonDev(@Value("classpath:/redisson-dev.yaml") Resource configResource) throws IOException {
       Config config = Config.fromYAML(configResource.getInputStream());
       return Redisson.create(config);
   }
    @Bean(destroyMethod = "shutdown")
    @Profile("test")
    public RedissonClient redissonTest(@Value("classpath:/redisson-test.yaml") Resource configResource) throws IOException {
        Config config = Config.fromYAML(configResource.getInputStream());
        return Redisson.create(config);
    }

    @Bean(destroyMethod = "shutdown")
    @Profile("prod")
    public RedissonClient redissonProd(@Value("classpath:/redisson-prod.yaml") Resource configResource) throws IOException {
        Config config = Config.fromYAML(configResource.getInputStream());
        return Redisson.create(config);
    }

}